
const { Router } = require('express')
let router = new Router()
let bodyParser = require("body-parser");
const agencyModel = require('../models/agencyModel')
const likeModel = require('../models/likeModel')

router.get('/agencyInfo', async (req, res) => {
  const { aid, user_id } = req.query
  if (aid) {
    let agencyItem = await agencyModel.findOne({ aid })
    if (agencyItem) {
      res.send({ success: true, data: agencyItem })
    } else {
      res.send({ success: false, message: '查询失败' })
    }
  } else if (user_id) {
    let AllagencyInfo = await agencyModel.find({ allow:'true',role:'agency'})
    if (AllagencyInfo) {
      let likeInfo = await likeModel.find({ user_id, status: 1 })
      if (likeInfo) {
        let newLikeInfo = likeInfo.map((item) => {
          return item.aid
        })
        let newAgencyList =  JSON.parse(JSON.stringify(AllagencyInfo))
        newAgencyList.forEach(item => {
            if(newLikeInfo.includes(item.aid)){
                item.status = 1
            }else{
                item.status = 2
            }
        })
       res.send({ success: true ,data: newAgencyList})
      }
    }
  } else {
    let AllagencyInfo = await agencyModel.find()
    if (AllagencyInfo) {
      res.send({ success: true, data: AllagencyInfo })
    } else {
      res.send({ success: false, message: '查询失败' })
    }
  }
})

router.post('/agencyLogin', bodyParser.json(), async (request, response) => {
  //1.获取用户输入
  const { mail, apassword } = request.body
  try {
    let findResult = await agencyModel.findOne({ mail, apassword })
    if (findResult) {
      response.send({ success: true, data: findResult })
    } else {
      response.send({ success: false, message: '登陆失败' })
    }
  }
  catch {
    response.send('网络不稳定，请稍后再试')
  }
})

// 添加机构
router.post('/agency/add', async (req, res) => {
  try {
    let aid = new Date().getTime();
    let result = await agencyModel.create({
      aid,
      ...req.body
    });
    res.send({ success: true, data: result })
  }
  catch (e) {
    res.send({
      success: false,
      msg: 'false' + e
    })
  }
})

// 更新机构
router.post('/agency/update', async (req, res) => {
  try {
    let result = '';
    if ((typeof req.body.allow) === 'boolean' && !req.body.real_name) {
    result = await agencyModel.updateOne(
      {
        aid: req.body.aid
      },
      {
        allow: req.body.allow
      }
    )
  } else {
    result = await agencyModel.updateOne(
      {
        aid: req.body.aid
      },
      req.body
    )
  }
   res.send({ success: true, data: result })
  }
  catch(e) {
    res.send({
      success: false,
      msg: 'false' + e
    })
  }
})

// 删除机构
router.get('/agency/delete', async (req, res) => {
  try {
    let result = await agencyModel.deleteOne({
      aid: req.query.aid
    });
    res.send({ success: true, data: result })
  }
  catch (e) {
    res.send({
      success: false,
      msg: 'false' + e
    })
  }
})

module.exports = router